<template>
<!--首页-->
  <div>
    <headers></headers>
    <div class="padding88">
      <div class="auto padding250" v-if="courses.length > 0">
        <p class="title">以下是有关"<span>{{this.keyWord}}</span>"的全部课程</p>
        <course :courses = "courses" :online="1" v-bind:isVip="isVip"></course>
        <div class="pageout"><Page :total="total" :current="pageNumber" :pageNumber-size="size" @on-change="changePage"/></div>
      </div>
      <div class="auto padding250" v-else >
        <p class="title">以下是有关"<span>{{this.keyWord}}</span>"的全部课程</p>
        <nodata tip="暂时还没有课程哦"></nodata>
      </div>
    </div>
    <asides></asides>
    <Spin fix v-show="isSpinShow" >
      <div class="loader">
        <Icon type="ios-loading" size=26 class="demo-spin-icon-load"></Icon>
        <div class="font14">加载中...</div>
      </div>
    </Spin>
    <footers></footers>
  </div>
</template>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less" rel="stylesheet/less">
  /* 旋转效果 */
  .demo-spin-icon-load{
    animation: ani-demo-spin 1s linear infinite;
  }
  @keyframes ani-demo-spin {
    from { transform: rotate(0deg);}
    50% { transform: rotate(180deg);}
    to { transform: rotate(360deg);}
  }
  /* 假如内容过长，一屏放不下，滚动条下拉覆盖不全 */
  .ivu-spin-fix {
    position: fixed;
  }

  .auto{
    .title{
      text-align: left;
      font-size: 16px;
      color: #333;
      margin: 30px 0;
      padding:15px 20px;
      background-color:#fff;
      span{
        color:#957960;
      }
    }
  }
</style>
<script type="text/ecmascript-6">
  import course from '../template/course'
  import asides from '../template/aside'
  import headers from '../template/header'
  import footers from '../template/footer'
  import nodata from '../template/null'
  import {centreInfoWeb, searchWeb} from '../../http'
  import {getStore} from "../../storage";
    export default {
        name: 'index',
        props: {},
        data() {
            return {
              keyWord:this.$route.query.keyWord,  //搜索关键词
              pageNumber:1,     //页码
              size:16,          //每页个数
              total:0,          //总数
              courses:'',       //课程
              isVip:0 ,         //是否Vip免费
              isSpinShow:false,
            };
        },
        methods: {
          //获取搜索结果
          getSearch(){
            searchWeb({keyWord: this.keyWord, pageNumber: this.pageNumber,pageSize:this.size}).then(res => {
              this.isSpinShow = false;
              if (res.code === 200) {
                this.courses = res.data.list.rows;
                this.pageNumber = res.data.list.pageNumber;
                this.total = res.data.list.total;
                this.isVip = res.data.isVip;
              } else {
                this.$Notice.warning({
                  title: '系统错误',
                  desc: '请刷新页面',
                  duration: 3,
                });
              }
            }).catch()
          },
          //页码切换
          changePage(pageNumber){
            this.pageNumber = pageNumber;
            if(this.isSpinShow === false) {
              this.isSpinShow = true;
              if (getStore('token') !== null && getStore('token') !== undefined && getStore('token') !== '') {
                //获取用户信息
                centreInfoWeb().then(res => {
                  if (res.code === 200) {
                    this.getSearch();
                  } else {
                    this.getSearch();
                  }
                }).catch()
              } else {
                this.getSearch();
              }
            }
          }
        },
        created() {
          if(this.isSpinShow === false) {
            this.isSpinShow = true;
            if (getStore('token') !== null && getStore('token') !== undefined && getStore('token') !== '') {
              //获取用户信息
              centreInfoWeb().then(res => {
                if (res.code === 200) {
                  this.getSearch();
                } else {
                  this.getSearch();
                }
              }).catch()
            } else {
              this.getSearch();
            }
          }
        },
        components: {
          asides,
          headers,
          footers,
          course,
          nodata
        }
    }
</script>
